<template>
  <div class="class-manage-container">
    <div class="page-header">
      <h2>班级管理</h2>
      <el-button type="primary" @click="handleAdd">添加班级</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="className" label="班级名称" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑班级对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form ref="formRef" :model="classForm" :rules="rules" label-width="100px">
        <el-form-item label="班级名称" prop="className">
          <el-input v-model="classForm.className" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave(formRef)">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Class } from '@/types/public'
import { getClasses, addClass, updateClass, deleteClass } from '@/api/admin'

// 表格数据
const tableData = ref<Class[]>([])
const loading = ref(false)

// 班级表单
const classForm = ref<{
  className: string
  oldClassName?: string
}>({
  className: ''
})

// 对话框控制
const dialogVisible = ref(false)
const dialogTitle = ref('添加班级')
const formRef = ref<FormInstance>()
const isEdit = ref(false)

// 表单规则
const rules = ref<FormRules>({
  className: [{ required: true, message: '请输入班级名称', trigger: 'blur' }]
})

// 加载表格数据
const loadTableData = async () => {
  loading.value = true
  try {
    const res = await getClasses()
    if (res.status) {
      tableData.value = res.data
    } else {
      ElMessage.error(res.message)
    }
  } catch (error) {
    console.error(error)
    ElMessage.error('获取班级列表失败')
  } finally {
    loading.value = false
  }
}

// 添加班级
const handleAdd = () => {
  dialogTitle.value = '添加班级'
  classForm.value = { className: '' }
  isEdit.value = false
  dialogVisible.value = true
}

// 编辑班级
const handleEdit = (row: Class) => {
  dialogTitle.value = '编辑班级'
  classForm.value = { 
    className: row.className,
    oldClassName: row.className
  }
  isEdit.value = true
  dialogVisible.value = true
}

// 删除班级
const handleDelete = (row: Class) => {
  ElMessageBox.confirm('确定要删除该班级吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await deleteClass(row.className)
      if (res.status) {
        ElMessage.success('删除成功')
        loadTableData()
      } else {
        ElMessage.error(res.message)
      }
    } catch (error) {
      console.error(error)
      ElMessage.error('删除班级失败')
    }
  })
}

// 保存班级
const handleSave = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      try {
        let res
        if (isEdit.value) {
          // 编辑班级
          res = await updateClass(classForm.value.oldClassName!, classForm.value.className)
        } else {
          // 添加班级
          res = await addClass(classForm.value.className)
        }
        
        if (res.status) {
          ElMessage.success(isEdit.value ? '更新成功' : '添加成功')
          dialogVisible.value = false
          loadTableData()
        } else {
          ElMessage.error(res.message)
        }
      } catch (error) {
        console.error(error)
        ElMessage.error(isEdit.value ? '更新班级失败' : '添加班级失败')
      }
    }
  })
}

// 页面加载时获取班级列表
onMounted(() => {
  loadTableData()
})
</script>

<style scoped>
.class-manage-container {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
